﻿using System;

using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using System.Collections;
using System.IO;

namespace CHMReader
{
    public partial class OpenFileDialog : Form
    {
        public string FileName;

        public OpenFileDialog()
        {
            InitializeComponent();
            FillTree();
        }

        private void ExpandNode(TreeNodeCollection ParentNode, string RootDir)
        {
            DirectoryInfo DI = new DirectoryInfo(RootDir);

            FileInfo[] Files = DI.GetFiles("*.chm");
            ArrayList FileSorted = new ArrayList(Files.Length);
            foreach (FileInfo FI1 in Files)
            {
                FileSorted.Add(FI1);
            }
            FileSorted.Sort(new myFileSort());

            foreach (FileInfo FI2 in FileSorted)
            {
                TreeNode tn = new TreeNode(FI2.Name);
                tn.Tag = FI2.FullName;
                //tn.ImageIndex = 6;
                //tn.SelectedImageIndex = 5;
                ParentNode.Add(tn);
                //ExpandNode(tn.Nodes, FI2.FullName);
            }

            DirectoryInfo[] Dirs = DI.GetDirectories();

            // Sort Dirs
            ArrayList DirSorted = new ArrayList(Dirs.Length);
            foreach (DirectoryInfo DI1 in Dirs)
            {
                DirSorted.Add(DI1);
            }
            DirSorted.Sort(new myDirSort());

            foreach (DirectoryInfo DI2 in DirSorted)
            {
                TreeNode tn = new TreeNode(DI2.Name);
                tn.Tag = DI2.FullName;
                //tn.ImageIndex = 6;
                //tn.SelectedImageIndex = 5;
                ParentNode.Add(tn);
                ExpandNode(tn.Nodes, DI2.FullName);
            }
        }

        private void FillTree()
        {
            treeView1.Nodes.Clear();
            Cursor.Current = Cursors.WaitCursor;
            ExpandNode(treeView1.Nodes, @"\");
            Cursor.Current = Cursors.Default;
            treeView1.SelectedNode = treeView1.Nodes[0];
        }

        private void mnuSelect_Click(object sender, EventArgs e)
        {
            string fn = treeView1.SelectedNode.Tag.ToString();
            if (fn.EndsWith(".chm"))
            {
                DialogResult = DialogResult.OK;
                FileName = fn;
                Close();
            }
        }

        private void mnuCancel_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
            Close();
        }
    }

    public class myDirSort : IComparer
    {
        // Calls CaseInsensitiveComparer.Compare with the parameters reversed.
        int IComparer.Compare(Object ax, Object ay)
        {
            DirectoryInfo x = (DirectoryInfo)ax;
            DirectoryInfo y = (DirectoryInfo)ay;
            return x.Name.CompareTo(y.Name);
        }
    }
    public class myFileSort : IComparer
    {
        // Calls CaseInsensitiveComparer.Compare with the parameters reversed.
        int IComparer.Compare(Object ax, Object ay)
        {
            FileInfo x = (FileInfo)ax;
            FileInfo y = (FileInfo)ay;
            return x.Name.CompareTo(y.Name);
        }
    }
}